package com.example.demo.controller;

import com.example.demo.modal.Physical;
import com.example.demo.service.PhysicalService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
@RequestMapping("/physical")
public class PhysicalController {
    @Autowired
    PhysicalService physicalService;

    @GetMapping
    String physical(){
        return "physical";
    }

    @PostMapping("/data")
    @ResponseBody
    public List<Physical> Object_data(){
        return physicalService.findAll();
    }

    @PostMapping("/max")
    @ResponseBody
    public int maxId(){
        return physicalService.selectMaxId();
    }

    @PostMapping("/save")
    @ResponseBody
    public String save(@RequestBody Physical physical){
        physicalService.save(physical);
        return "添加成功！";
    }

    @PostMapping("/delete")
    @ResponseBody
    public String delete(@RequestParam("id") int id){
        physicalService.deleteById(id);
        return "删除成功！";
    }

    @PostMapping("/edit")
    @ResponseBody
    public String edit(@RequestBody Physical physical){
        physicalService.update(physical);
        return "ok";
    }

    @PostMapping("/find/id")
    @ResponseBody
    public List<Physical> find_id(@RequestParam("id") int id){
        return physicalService.findById(id);
    }

    @PostMapping("/find/name")
    @ResponseBody
    public List<Physical> find_name(@RequestParam("name") String name){
        return physicalService.findByName(name);
    }

    @PostMapping("/find/parentid")
    @ResponseBody
    public List<Physical> find_abilities(@RequestParam("parentId") int pid){
        return physicalService.findByPId(pid);
    }
}
